<?


JCAT_Package::ImportClass( ROOT_PATH.'Classes/Coder/class.CoderDocBase.php') ;

class JCATMVCObjectContainerDoc extends JCAT_ArchiveDocument 
{
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function JCATMVCObjectContainerDoc( $sName, $nAllowChildObjectType=self::OBJECT_TYPE_ALL )
	{
		$this->JCAT_ArchiveDocument( $sName ) ;
		
		$this->Set('Children',array()) ;
		$this->Set('GlobalNames',array()) ;
		
		$this->nAllowChildObjectType = $nAllowChildObjectType ;
	}
	
	
	/**
	 * what's this
	 * 
	 * @access	public
	 * @return	void
	 */
	public function GetObjectTypes()
	{
		return $this->nAllowChildObjectType ;
	}
	
	/**
	 * 添加一个 UICtrl 作为 子控件
	 * 
	 * @access	public
	 * @param	$sPath		string			what's this
	 * @return	bool
	 */
	public function AddUICtrl( $aLoadingDoc, $sRelationPath )
	{
		// 得到对象的 Class 名称
		$aObjectClassName = $aLoadingDoc->GetObjectClass() ;
		if( JCAT_Global::HasImplementedTo($aObjectClassName,'JCAT_IController') )
		{
			$nDocType = self::OBJECT_TYPE_CONTROLLER ;
			$sDocType = 'JCAT_IController' ;
		}
		else if( JCAT_Global::HasImplementedTo($aObjectClassName,'JCAT_IView') )
		{
			$nDocType = self::OBJECT_TYPE_VIEW ;
			$sDocType = 'JCAT_IView' ;
		}
		else if ( JCAT_Global::HasImplementedTo($aObjectClassName,'JCAT_IDocument') )
		{
			$nDocType = self::OBJECT_TYPE_DOCUMENT ;
			$sDocType = 'JCAT_IDocument' ;
		}
		else 
		{
			$nDocType = 0 ;
		}
		
		// 判断文档类型
		if( ($nDocType&$this->nAllowChildObjectType)===0 )
		{
			JCAT_Gtk::MsgDlg(JCAT_Language::Sentence('无效的文档类型。')) ;
			return false ;
		}
		
		// 取得已有 
		$arrChildren = $this->Get('Children') ;
		JCAT_ASSERT::ASSERT_ARRAY($arrChildren) ;
		
		// 检查 重复
		foreach($arrChildren as $arrChild)
		{
			JCAT_ASSERT::ASSERT_ARRAY($arrChild) ;
			
			// 遇到重复
			if( $arrChild[TreeJCATMVCObject::CLM_PATH]==$sRelationPath )
			{
				return false ;
			}
		}

		// 加入
		$arrNewChild[TreeJCATMVCObject::CLM_TYPE] = $sDocType ;
		$arrNewChild[TreeJCATMVCObject::CLM_ICON] = $aLoadingDoc->GetKindIcon(24) ;
		$arrNewChild[TreeJCATMVCObject::CLM_NAME] = $aLoadingDoc->GetObjctName() ;
		$arrNewChild[TreeJCATMVCObject::CLM_DOCNAME] = '' ;
		$arrNewChild[TreeJCATMVCObject::CLM_GLOBAL_NAME] = '' ;
		$arrNewChild[TreeJCATMVCObject::CLM_ACTIVE] = false ;
		$arrNewChild[TreeJCATMVCObject::CLM_PATH] = $sRelationPath ;
		$arrChildren[] = $arrNewChild ;

		// 保存
		$this->Set('Children',$arrChildren) ;

		return true ;
	}

	/**
	 * 将 子UICtrl 加载到 TreeView 上
	 * 
	 * @access	public
	 * @param	$aTreeModel				GtkTreeModel		what's this
	 * @param	$aParentItem=null		GtkTreeIter			what's this
	 * @return	void
	 */
	public function LoadChildrenObject(GtkTreeModel $aTreeModel, GtkTreeIter $aParentItem=null)
	{
		$arrChildren = $this->Get('Children') ;
		if(!$arrChildren)
		{
			return ;
		}

		JCAT_ASSERT::ASSERT_ARRAY($arrChildren) ;

		// 
		foreach($arrChildren as $arrChild)
		{
			// 插入 Item
			$aChildItem = $aTreeModel->append($aParentItem,array(
					$arrChild[TreeJCATMVCObject::CLM_TYPE]
					, $arrChild[TreeJCATMVCObject::CLM_ICON]
					, $arrChild[TreeJCATMVCObject::CLM_NAME]
					, $arrChild[TreeJCATMVCObject::CLM_DOCNAME]
					, $arrChild[TreeJCATMVCObject::CLM_GLOBAL_NAME]
					, $arrChild[TreeJCATMVCObject::CLM_ACTIVE]
					, $arrChild[TreeJCATMVCObject::CLM_PATH]
			)) ;

			// 递归加载 子对象的 子对象  :::::::::::::::::::::::::::::::::

			// 将记录中的 相对路径  换为 绝对路径
			$sRelativePath = $arrChild[TreeJCATMVCObject::CLM_PATH] ;
			if( JCAT_FSO::IsAbsolutePath($sRelativePath) and is_file($sRelativePath) )
			{
				$sChildPath =& $sRelativePath ;
			}
			else
			{
				$sChildPath = dirname($this->GetPath()).'/'.$sRelativePath ;
			}

			// 恢复 子控件 的文档
			$aDocument = CoderDocBase::WakeupDocument($sChildPath) ;
			if( $aDocument )
			{
				JCAT_ASSERT::ASSERT_INSTANCE($aDocument,'IJCATMVCObject') ;
				$aChildDocument = $aDocument->GetObjectChildrenDoc() ;
				JCAT_ASSERT::ASSERT_INSTANCE($aChildDocument,__CLASS__) ;
				$aChildDocument->LoadChildrenObject($aTreeModel,$aChildItem) ;					
			}
			else 
			{
				// $aTreeModel->append($aChildItem,array('','-','-','-',JCAT_Language::Sentence('UICtrl控件存在错误'))) ;
			}
		}
	}



	const OBJECT_TYPE_CONTROLLER = 1 ;
	const OBJECT_TYPE_DOCUMENT = 2 ;
	const OBJECT_TYPE_VIEW = 4 ;
	const OBJECT_TYPE_ALL = 7 ;
	
	private $nAllowChildObjectType = 0 ;
}
?>